//
//  KATAlertWindow.m
//  KATFramework
//
//  Created by Kantice on 14-7-17.
//  Copyright (c) 2014年 KatApp. All rights reserved.
//

#import "KATAlertWindow.h"



@interface KATAlertWindow ()

///标题
@property(nonatomic,retain) KATLabel *titleLabel;

///内容
@property(nonatomic,retain) KATLabel *infoLabel;

@end



@implementation KATAlertWindow



//获取实例（默认样式，只有标题）
+ (instancetype)windowWithTitle:(NSString *)title
{
    return [self windowWithTitle:title titleStyle:nil info:nil infoStyle:nil];
}


//获取实例（只有标题和样式）
+ (instancetype)windowWithTitle:(NSString *)title andStyle:(KATTextStyle *)style
{
    return [self windowWithTitle:title titleStyle:style info:nil infoStyle:nil];
}


//获取实例（默认样式，只有提示）
+ (instancetype)windowWithInfo:(NSString *)info
{
    return [self windowWithTitle:nil titleStyle:nil info:info infoStyle:nil];
}


//获取实例（只有提示和样式）
+ (instancetype)windowWithInfo:(NSString *)info andStyle:(KATTextStyle *)style
{
    return [self windowWithTitle:nil titleStyle:nil info:info infoStyle:style];
}


//获取实例（默认样式，带标题和提示）
+ (instancetype)windowWithTitle:(NSString *)title andInfo:(NSString *)info
{
    return [self windowWithTitle:title titleStyle:nil info:info infoStyle:nil];
}


//获取实例(全参数)
+ (instancetype)windowWithTitle:(NSString *)title titleStyle:(KATTextStyle *)tStyle info:(NSString *)info infoStyle:(KATTextStyle *)iStyle
{
    KATAlertWindow *window=[[[self alloc] init] autorelease];
    
    [window initData];
    
    window.isTapShadowToHide=YES;
    window.isTapContentToHide=YES;
    window.isBlurStyle=YES;
    window.showMode=KATPopWindowAnimationTypeBlurScale;
    window.hideMode=KATPopWindowAnimationTypeAlpha;
    window.cornerRadius=10.0;
    
    [window initWindow];
    
    
    //样式
    window.contentWidth=sqrt(MIN(SCREEN_MIN,500.0))*15.0;
    window.contentPadding=sqrt(MIN(SCREEN_MIN,500.0))*0.8;
    
    //默认的样式
    if(!tStyle)
    {
        tStyle=[KATTextStyle textStyle];
        
        tStyle.textFont=[UIFont boldSystemFontOfSize:window.contentPadding*1.16];
        tStyle.textColor=KCOLOR_NIGHT;
        tStyle.hasDateStyle=NO;
        tStyle.textAlignment=NSTextAlignmentCenter;
        tStyle.marginTop=1;
        tStyle.marginLeft=1;
        tStyle.marginRight=1;
        tStyle.marginBottom=1;
    }
    
    if(!iStyle)
    {
        iStyle=[KATTextStyle textStyle];
        
        iStyle.textFont=[UIFont systemFontOfSize:window.contentPadding*1.04];
        iStyle.textColor=KCOLOR_DARK;
        iStyle.hasDateStyle=NO;
        iStyle.textAlignment=NSTextAlignmentCenter;
        iStyle.marginTop=1;
        iStyle.marginLeft=1;
        iStyle.marginRight=1;
        iStyle.marginBottom=1;
    }
    
    window.titleLabel=[KATLabel labelWithFrame:CGRectZero andStyle:nil];
    window.infoLabel=[KATLabel labelWithFrame:CGRectZero andStyle:nil];
    
    [window.content addSubview:window.titleLabel];
    [window.content addSubview:window.infoLabel];
    
    window.title=title;
    window.titleStyle=tStyle;
    window.info=info;
    window.infoStyle=iStyle;
    
    [window updateWindow];
    
    return window;
}

- (void)setTitle:(NSString *)title
{
    if(title!=_title)
    {
        [_title release];
        _title=[title copy];
        
        [self updateWindow];
    }
}



- (void)setInfo:(NSString *)info
{
    if(info!=_info)
    {
        [_info release];
        _info=[info copy];
        
        [self updateWindow];
    }
}


- (void)setTitleStyle:(KATTextStyle *)titleStyle
{
    if(titleStyle!=_titleStyle)
    {
        [_titleStyle release];
        _titleStyle=[titleStyle retain];
        
        [self updateWindow];
    }
}


- (void)setInfoStyle:(KATTextStyle *)infoStyle
{
    if(infoStyle!=_infoStyle)
    {
        [_infoStyle release];
        _infoStyle=[infoStyle retain];
        
        [self updateWindow];
    }
}


- (void)setContentWidth:(float)contentWidth
{
    _contentWidth=contentWidth;
    
    [self updateWindow];
}


- (void)setContentPadding:(float)contentPadding
{
    _contentPadding=contentPadding;
    
    [self updateWindow];
}


- (void)show
{
    [self updateWindow];
    
    [super show];
}


//更新窗口
- (void)updateWindow
{
    _titleLabel.frame=CGRectMake(_contentPadding, _contentPadding, _contentWidth-2*_contentPadding, 0);
    
    _titleLabel.text=_title;
    
    if(_title)
    {
        _titleLabel.style=_titleStyle;
        
        [_titleLabel adjustFrame];
    }
    
    _infoLabel.frame=CGRectMake(_contentPadding, _titleLabel.frame.origin.y+_titleLabel.frame.size.height, _contentWidth-2*_contentPadding, 0);
    
    _infoLabel.text=_info;
    
    if(_info)
    {
        _infoLabel.style=_infoStyle;
        [_infoLabel adjustFrame];
    }
    
    self.contentSize=CGSizeMake(_contentWidth, _infoLabel.frame.origin.y+_infoLabel.frame.size.height+_contentPadding);
}


//调整窗口(方向改变系统通知处理)
- (void)resizeWindow
{
    [super resizeWindow];
}



- (void)dealloc
{    
    [_infoLabel release];
    [_titleLabel release];
    [_title release];
    [_info release];
    [_titleStyle release];
    [_infoStyle release];
    
    [super dealloc];
}


@end



